TARGET = application
BUILD_DIR = build
OPT = -Og
DEBUG = 0
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
SZ = $(PREFIX)size
CP = $(PREFIX)objcopy
AR = $(PREFIX)ar
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
CPU = -mcpu=cortex-m0
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
C_DEFS = \
-DSTM32F030xC \
-DUSE_HAL_DRIVER

ASM_SOURCES = 
# Middlewares/rt-thread/libcpu/arm/cortex-m0/context_gcc.s

C_SOURCES = \
board.c \
drv_common.c \
flash.c \
gpio.c \
i2c.c \
iwdg.c \
m24c64.c \
ram.c \
rtc.c \
spi.c \
tim.c \
uart.c \
adc.c \
stm32f0xx_it.c \
../Main/main.c \
../Application/filesystem/lfs_if.c \
../Application/com/com.c \
../Application/com/com_device.c \
../Application/iap_update/iap_update.c \
../Application/ota/ota.c \
../Application/stdcrc16/stdcrc16.c \
../Application/proc_bar/proc_bar.c \
../Application/ymodem/ymodem.c \
../Application/modbus/modbus.c \
../Application/std2007/std2007.c \
../Application/msh_cmd/ymodem_cmd.c

C_INCLUDES = \
-I../Drivers/CMSIS/Include \
-I../Drivers/CMSIS/Device/ST/STM32F0xx/Include \
-I../Drivers/STM32F0xx_HAL_Driver/Inc \
-I../Middlewares/rt-thread/include \
-I../Middlewares/rt-thread/include/libc \
-I../Middlewares/rt-thread/components/finsh \
-I../Middlewares/littlefs \
-I../Application/filesystem \
-I../Application/com \
-I../Application/iap_update \
-I../Application/ota \
-I../Application/stdcrc16 \
-I../Application/proc_bar \
-I../Application/ymodem \
-I../Application/modbus \
-I../Application/std2007 \
-I.

ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=c99
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

LDSCRIPT = link.lds
LIBS =  -lstm32f030xc -lrtt -llfs -lc -lm -lnosys
LIBDIR = -L../Lib
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).bin
ifeq ($(shell uname), Linux)
	@./app_packet 2333
else
	@./app_packet.exe 2333
endif
	@mv xldz_application.bin ../
	@mv xldz_mix.bin ../

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	@$(info $(notdir $@))
	@$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	@$(info $(notdir $@))
	@$(AS) -c $(ASFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(BUILD_DIR) $(OBJECTS) Makefile
	@$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	@$(SZ) $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	@$(BIN) $< $@
	@$(info $(notdir $@))

$(BUILD_DIR):
	@mkdir -p $@

clean: 
	@rm -rf $(BUILD_DIR)

rebuild: clean library all

-include $(wildcard $(BUILD_DIR)/*.d)
